package com.example.springaop.myAop.dynamicProxy;

import com.example.springaop.myAop.MyLogger;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 增加一个 ObjectInterceptor.java 类
 */
public class ObjectInterceptor implements InvocationHandler {

    //目标类
    private Object target;

    //切面类（这里指 日志类）
    private MyLogger logger;

    public ObjectInterceptor(Object target, MyLogger logger) {
        this.target = target;
        this.logger = logger;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //参数日志
        logger.showParam((int) args[0], (int) args[1]);
        //System.out.println(args[0] + "--" + args[1]);
        int result = (int) method.invoke(target, args);
        //结果日志
        logger.showResult(result);

        return result;
    }
}
